package webdev.adapters;

import java.util.ArrayList;

import webdev.danagtravel.PlaceDetailActivity;
import webdev.dto.DiaDiemDTO;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import dev.danang.R;

public class ListAmThucAdapter extends BaseAdapter {

	private ArrayList<DiaDiemDTO> datas;
	private LayoutInflater mInflater;
	private Context mContext;
	
	public ListAmThucAdapter(Context context) {
		datas = new ArrayList<DiaDiemDTO>();
		this.mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void addData(DiaDiemDTO places) {
		datas.add(places);
	}
	
	@Override
	public int getCount() {
		return datas.size();
	}
	
	@Override
	public DiaDiemDTO getItem(int position) {
		return datas.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		
		if(convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_list_detail, null);
			
			viewHolder.mImgView = (ImageView) convertView.findViewById(R.id.img_of_list_detail);
			viewHolder.mTxtName = (TextView) convertView.findViewById(R.id.txt_name_place_of_list_detail);
			viewHolder.mTxtAddress = (TextView) convertView.findViewById(R.id.txt_address_place_of_list_detail);
			
			viewHolder.mLayoutItemList = (RelativeLayout) convertView.findViewById(R.id.layout_item_list);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		viewHolder.mTxtName.setText(getItem(position).getTitle());
		viewHolder.mTxtAddress.setText(getItem(position).getAddress());
		
		viewHolder.mLayoutItemList.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				goToDetail(position);
			}
		});
		
		/*int drawResId = mContext.getResources().getIdentifier(getItem(position).getImageName(), "drawable", mContext.getPackageName());
		viewHolder.mImgView.setImageResource(drawResId);*/
		
		return convertView;
	}
	
	private class ViewHolder {
		private ImageView mImgView;
		private TextView mTxtName;
		private TextView mTxtAddress;
		private RelativeLayout mLayoutItemList;
	}
	
	private void goToDetail(int position) {
		Intent goToDetailPlace = new Intent(mContext, PlaceDetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putParcelable("places", datas.get(position));
		goToDetailPlace.putExtras(bundle);
		mContext.startActivity(goToDetailPlace);
	}
	
}
